<?php

namespace app\award;

use app\models\NewLevel;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\OrderActive;
use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\Queue;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;
use app\util\Util;

class Award
{
    protected $order;
    protected $user;

    protected $set;

    protected $total_pay;

    // 第一个产品ID
    protected $first_product_id;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $this->total_pay = $this->order->total_pay - $this->order->total_freight;

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $this->first_product_id = $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        $is_material = ProductArea::checkMaterial($product_id);
        $is_replacement = ProductArea::checkReplacement($product_id);


        // 报单
        if ($is_bao) {
            $this->bao();
        }

        // 材料区
        if ($is_material) {
            $this->material();
        }

        // 置换
        if ($is_replacement) {

        }
    }

    private function _setProduct($diff_key)
    {
        $order_items = OrderItem::find()
            ->where(['order_id' => $this->order->id])
            ->all();

        $sum = 0;

        foreach ($order_items as $item) {
            $product = SetProductCommission::findOne(['product_id' => $item->product_id])->toArray();
            $sum += ($product[$diff_key]) * $item['quantity'];
        }

        return $sum;
    }

    // 报单区
    public function bao()
    {
        $award_name1 = '直推奖';
        $award_type1 = 11;

        $award_name2 = '间推奖';
        $award_type2 = 12;

        // 低推高, 一半
        $is_low = false;
        $amount = $this->order->total_pay - $this->order->total_freight;
        if ($amount >= $this->set->svip2) {
            $is_low = true;
        }

        $r1 = Chain::getReferrer($this->user->id, true);
        if ($r1) {
            if ($r1->level >= UserLevel::$keys['svip1']) {
                $commission = $this->_setProduct('r1');
                if ($is_low && $r1->level == UserLevel::$keys['svip1']) {
                    $commission /= 2;
                }
                $this->saveCommission($r1->id, $commission, $award_type1, $award_name1);
            }

            $r2 = Chain::getReferrer($r1->id, true);
            if ($r2) {
                if ($r2->level >= UserLevel::$keys['svip1']) {
                    $commission = $this->_setProduct('r2');
                    if ($is_low && $r2->level == UserLevel::$keys['svip1']) {
                        $commission /= 2;
                    }
                    $this->saveCommission($r2->id, $commission, $award_type2, $award_name2);
                }
            }
        }
    }

    // 材料奖
    public function material()
    {
        $award_name1 = '代理费';
        $award_type1 = 21;

        $award_name2 = '平级奖';
        $award_type2 = 22;

        $has_svip1 = false;
        $has_svip1_2 = false;
        $has_svip2 = false;
        $has_svip2_2 = false;

        $diff_svip1 = 0;
        $diff_svip2 = 0;

        $chain = Chain::getReferrerChain($this->order->user_id, [
            UserLevel::$keys['svip1'],
            UserLevel::$keys['svip2']
        ], true);

        foreach ($chain as $user) {
            if (((!$has_svip1 || !$has_svip1_2) && !$has_svip2) && $user->level == UserLevel::$keys['svip1']) {
                if ($diff_svip1 == 0) {
                    $diff_svip1 = $this->_setProduct('m1');
                    $this->saveCommission($user->id, $diff_svip1, $award_type1, $award_name1);
                    $has_svip1 = true;
                } else {
                    $this->saveCommission($user->id, $this->_setProduct('t1'), $award_type2, $award_name2);
                    $has_svip1_2 = true;
                }
            }
            if (((!$has_svip2 || !$has_svip2_2)) && $user->level == UserLevel::$keys['svip2']) {
                if ($diff_svip2 == 0) {
                    $diff_svip2 = $this->_setProduct('m2');
                    $diff_svip2 -= $diff_svip1;
                    $this->saveCommission($user->id, $diff_svip2, $award_type1, $award_name1);
                    $has_svip2 = true;
                } else {
                    $this->saveCommission($user->id, $this->_setProduct('t2'), $award_type2, $award_name2);
                    $has_svip2_2 = true;
                    break;
                }
            }
        }
    }

    // 保存佣金
    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {

            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->user->id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                $account->balance += $balance;
                $account->save();
            }
        }
    }

}